------------------ fgui require start ------------------
------------------  fgui require end  ------------------
local FlagSkin = Class("FlagSkin");

function FlagSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.ctl_seled.onChanged:Add(self.onChange_Ctl_seled,self);
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function FlagSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.ctl_seled = com:GetController("Ctl_seled");
    self.ldr_icon = com:GetChild("ldr_icon");
    self.text_Notunlocked = com:GetChild("Text_Notunlocked");
    ------------------  fgui变量绑定end  ------------------
end

function FlagSkin:init()
    self.idx = 0;
end

function FlagSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.ctl_seled.onChanged:Clear();
    ------------------  fgui事件移除end  ------------------
end

function FlagSkin:onChange_Ctl_seled(event) end

function FlagSkin.onChange_Ctl_seled(event) end

------------------  ↑ fgui事件句柄 ↑  ------------------

function FlagSkin:doSeled(seled)
    self.ctl_seled.selectedIndex = seled and 1 or 0;
end

function FlagSkin:isSeled()
    return self.ctl_seled.selectedIndex == 1;
end

function FlagSkin:isLocked()
    return self.ctl_seled.selectedIndex == 2;
end

function FlagSkin:onRender(data,seledFlagIndex)
    self.idx = data.f_Id;
    self.ldr_icon.url = logicMgr.GuildManager.getFlagUrl(self.idx);
    if not self:checkUnlock(data) then
        self.ctl_seled.selectedIndex = 2;
    elseif seledFlagIndex == data.f_Id then
        self.ctl_seled.selectedIndex = 1;
    else
        self.ctl_seled.selectedIndex = 0;
    end
end

function FlagSkin:checkUnlock(data)
    local limitTab = decodeJsonStrToTabel(data.f_Unlock) or {};
    if #limitTab == 0 then return true end;
    local guildLv = logicMgr.GuildManager.getUnionLv();
    for _ , v in ipairs(limitTab) do
        if v[1] == 1 and v[2] > guildLv then
            self.text_Notunlocked.text = string.format(getLanguage("GuildInvestment_1031",nil,"%s"), v[2]);
            return false;
        end

        if v[1] == 3 and v[2] > 999 then
            self.text_Notunlocked.text = string.format(getLanguage("GuildInvestment_1067",nil,"%s"), v[2]);
            return false;
        end
    end
    return true;
end

function FlagSkin:beforeDestroy() end

return FlagSkin;
